import { useReducer } from 'react';

/**
 * 定义初始状态信息
 */
interface IstateType {
  contactsSwitchList: Record<string, any>[];
  supplierInfoObj: Record<string, any>;
  contactsCurrent: number;
  qualifyDataList: Record<string, any>[];
  memoirDataList: Record<string, any>[];
  supplierCompannyName: string;
}

/**
 * reducer 枚举类型
 */
enum ISupplierActionEnum {
  CHECKLIST_CONTACTS = 'contactsSwitchList',
  SAVEINFO_SUPPLIEROBJ = 'saveinfoSupplierObj',
  CONTACTS_CURRENT = 'contactsCurrent',
  QUALIFYDATA_LIST = 'qualifyDataList',
  MEMOIRDATA_LIST = 'memoirDataList',
  SUPPLIER_NAME = 'supplierCompannyName',
}

/**
 * 定义页面操作匹配
 * @type 操作类型
 * @payload 传递数据
 */
interface IActionType {
  type: ISupplierActionEnum;
  payload?: any;
}

/** 定义供应商状态管理 */
const useSupplierReducer = () => {
  /** 初始化状态 */
  const initState: IstateType = {
    contactsSwitchList: [],
    supplierInfoObj: {},
    contactsCurrent: 0,
    qualifyDataList: [],
    memoirDataList: [],
    supplierCompannyName: '',
  };

  /** 状态管理逻辑处理 */
  const updateReducer = (state: IstateType, action: IActionType) => {
    switch (action?.type) {
      /** 保存供应商名称 */
      case ISupplierActionEnum.SUPPLIER_NAME:
        return {
          ...state,
          supplierCompannyName: action.payload,
        };
      /** 存储负面影响数据 */
      case ISupplierActionEnum.MEMOIRDATA_LIST:
        return {
          ...state,
          memoirDataList: action.payload,
        };

      /** 存储相关资质数据 */
      case ISupplierActionEnum.QUALIFYDATA_LIST:
        return {
          ...state,
          qualifyDataList: action.payload,
        };

      case ISupplierActionEnum.CONTACTS_CURRENT:
        return {
          ...state,
          contactsCurrent: action.payload,
        };
      case ISupplierActionEnum.CHECKLIST_CONTACTS:
        return {
          ...state,
          contactsSwitchList: action.payload,
        };
      case ISupplierActionEnum.SAVEINFO_SUPPLIEROBJ:
        return {
          ...state,
          supplierInfoObj: action.payload,
        };
      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, supplierDispath] = useReducer(updateReducer, initState);

  return {
    ...state,
    supplierDispath,
  };
};

export { useSupplierReducer, ISupplierActionEnum };
